/*
 * Copyright 2011 DidactiLab SAS
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * Date: 30 avr. 2011
 * Author: Mathieu LIGOCKI
 */
package com.didactilab.gwt.phprpc.rebind.phpgen;

import com.didactilab.gwt.phprpc.rebind.PhpTools;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JEnumConstant;
import com.google.gwt.core.ext.typeinfo.JEnumType;

public class PhpEnum extends PhpType {

	public PhpEnum(JEnumType type) {
		super(type);
	}

	@Override
	protected void getContents(TreeLogger logger, StringBuffer buffer)
			throws UnableToCompleteException {
		JEnumType type = getJavaType().isEnum();
		buffer.append("/**\n");
		buffer.append(" * @gwtname ").append(type.getQualifiedBinaryName()).append("\n");
		if (type.getEnclosingType() != null) {
			buffer.append(" * @enclosing ").append(type.getEnclosingType().getQualifiedBinaryName()).append("\n");
		}
		buffer.append(" */\n");
		buffer.append("class ").append(PhpTools.typeToString(type, true)).append(" extends Enum {\n");
		for (JEnumConstant constant : type.getEnumConstants()) {
			buffer.append("\tconst ").append(constant.getName()).append(" = ").append(constant.getOrdinal()).append(";\n");
		}
		buffer.append("}\n");
	}
	
}
